<?php

class View 
{
	protected $_vars;
	protected $isAutoDisplay = false;
	protected $theme = 'default';
	protected $moduleName = 'index';
	protected $actionName = 'index';
	protected $useLayout = true;
	protected $themeFile = '';
	protected $contentType = 'text/html';
	protected $charset = null;
	protected $isAjax = false;
	protected $place = array();
	
	public function __construct($config = null)
	{
		$this->isAutoDisplay = isset($config['isAutoDisplay']) ? 
			$config['isAutoDisplay'] : Config::getConfig('VIEW_IS_AUTO_DISPLAY');
		$this->theme = isset($config['theme']) ? 
			$config['theme'] : Config::getConfig('VIEW_THEME');
		$this->useLayout = isset($config['useLayout']) ? 
		    $config['useLayer'] : Config::getConfig('VIEW_IS_USE_LAYOUT');
	}
	
	public function assign($key, $value)
	{
		$this->_vars[$key] = $value;
	}

	private function _autoDisplay()
	{
	    $themeFile = '';
	    if(!empty($this->themeFile))
	    {
    	    $themeFile = APP_ROOT.'/view/'.$this->theme.'/page/'.$this->themeFile;
	    }
	    if($this->useLayout && !file_exists($themeFile))
	    {
	        $themeFile = APP_ROOT.'/view/'.$this->theme.'/page/'.$this->actionName.'layout.php';
	        if(!file_exists($themeFile) || !is_file($themeFile))
			{
			    $themeFile = APP_ROOT.'/view/'.$this->theme.'/layout.php';
			}
	    }
	    if(!file_exists($themeFile) || !is_file($themeFile))
	    {
    	    $themeFile = APP_ROOT.'/view/'.$this->theme.'/page/'.$this->moduleName.'/'.$this->actionName.Config::getConfig('VIEW_THEME_FILE_SUFFIX');
	    }
	    
		RegGlobal::set('END_TIME', Functions::microtime_float());
		RegGlobal::set('USE_TIME', RegGlobal::get('END_TIME') - RegGlobal::get('START_TIME'));
		
		$this->_sendContentTypeHeader();
		
		if(file_exists($themeFile) && is_file($themeFile))
		{
			@include $themeFile;
		}
		else
		{
		    echo $themeFile.' Not Found!';
			//找不到$this->moduleName模块$this->actionName功能的模板
		}
	}
	
	private function _sendContentTypeHeader()
	{
	    $charset = $this->charset;
	    empty($charset) && $charset = Config::getConfig('WEB_CHARSET');
		empty($charset) && $charset = 'utf-8';
		$contentType = $this->contentType;
		empty($contentType) && $contentType = 'text/html';
		header("Content-type:{$contentType};charset={$charset}");
	}
	
	public function display()
	{
	    if($this->isAutoDisplay){ return ; }
	    $this->_autoDisplay();
	}
	
	public function setThemeFile($themeFile = '')
	{
		$this->themeFile = $themeFile;
	}
	
	public function setContentType($contentType)
	{
	    $this->contentType = $contentType;
	}
	
	public function setCharset($charset)
	{
	    $this->charset = $charset;
	}
	
	public function import($file, $folder = 'import')
	{
		if(!empty($file))
		{
			$themeFile = APP_ROOT.'/view/'.$this->theme.'/'.$folder.'/'.$file.Config::getConfig('VIEW_THEME_FILE_SUFFIX');
			if(file_exists($themeFile) && is_file($themeFile))
			{
				@include $themeFile;
			}
		}
	}
	
	public function place($name)
	{
	    if(!empty($name) && isset($this->place[$name]))
	    {
	        $themeFile = APP_ROOT.'/view/'.$this->theme.'/'.$this->place[$name].Config::getConfig('VIEW_THEME_FILE_SUFFIX');
	        if(file_exists($themeFile) && is_file($themeFile))
			{
				@include $themeFile;
			}
	    }
	}
	
	public function setPlace($name, $tpl)
	{
	    !empty($name) && !empty($tpl) && $this->place[$name] = $tpl;
	}
	
	private function _getFileUniqid($file)
	{
	    return sprintf("%u", crc32($file));
	}
	
	public function css($fileName, $isFromCommon = false)
	{
	    $css = explode(',', $fileName.',');
	    foreach ($css as $cssfile)
	    {
	        $cssfile = trim($cssfile);
	        if(empty($cssfile)) continue;
    	    $url = $this->_staticFile($cssfile, 'css', $isFromCommon);
    	    if($url != null)
    	        echo '<link href="'.$url.'" rel="stylesheet" type="text/css" />';
	    }
	}
	
	public function js($fileName, $isFromCommon = false)
	{
	    $js = explode(',', $fileName.',');
	    foreach ($js as $jsfile)
	    {
	        $jsfile = trim($jsfile);
	        if(empty($jsfile)) continue;
    	    $url = $this->_staticFile($jsfile, 'js', $isFromCommon);
    	    if($url != null)
    	        echo '<script src="'.$url.'" type="text/javascript"></script>';
	    }
	}
	
	public function pic($pic, $isFromCommon = false, $hideNotFound = false)
	{
	    $url = null;
        if(!empty($pic))
        {
            $url = $this->_staticFile($pic, 'img', $isFromCommon);
        }
        //如果url不存在则调用默认图片
        if($url == null && !$hideNotFound)
        {
            $url = $this->_staticFile(Config::getConfig('VIEW_NO_PIC_FILE'), 'img', true);
            $url = $url == null ? $pic : $url;
        }
	    if($url != null)
	    {
	        echo $url;
	    } 
	}
	
	private function _staticFile($fileName, $type, $isFromCommon = false)
	{
	    if(!in_array($type, array('css', 'js', 'img'))) return null;
	    static $static = array();
		$domainRoot = Config::getConfig('WEB_DOMAIN_ROOT');
		$domainRoot = rtrim($domainRoot, '/').'/';
		$url = '';
		$file = '';
		if($isFromCommon)
		{
		    $url = $domainRoot.'common/'.$type.'/'.$fileName;
		    $file = PUBLIC_ROOT.'/common/'.$type.'/'.$fileName;
		}
		else 
		{
			$theme = Config::getConfig('VIEW_THEME');
			$url = $domainRoot.'skin/'.$theme.'/'.$type.'/'.$fileName;
			$file = PUBLIC_ROOT.'/skin/'.$theme.'/'.$type.'/'.$fileName;
		}
		if(!file_exists($file) || !is_file($file)) return null;
		
		$uniqid = $this->_getFileUniqid($url);
		if(!isset($static[$type][$uniqid]))
		{
		    $css[$type][$uniqid] = $url;
		    return $url;
		}
		return null;
	}
	
	public function setAutoDisplay($flag = true)
	{
		$this->isAutoDisplay = (bool)$flag;
	}
	
	public function setUseLayout($flag = true)
	{
	    $this->useLayout = $flag;
	}
	
	public function setIsAjax($flag = false)
	{
	    $this->isAjax = $flag;
	}
	
	public function isAjax()
	{
	    return $this->isAjax;
	}
	
	public function __set($name, $value)
	{
		$allowSetVars = array('isAutoDisplay', 'theme', 'moduleName', 'actionName');
		if(in_array($name, $allowSetVars))
		{
			$this->$name = $value;
		}
	}
	
	public function __get($name)
	{
		if(isset($this->_vars[$name]))
		{
			return $this->_vars[$name];
		}
		else
		{
			return '';
		}
	}
	
	public function __destruct()
	{
	    if($this->isAutoDisplay)
		    $this->_autoDisplay();
	}
}